import { createApp } from 'vue'
import pinia from '@/store/index';
import { Page } from '@/store/page';
import App from './App.vue'
import router from './router';
import { i18n } from '@/languages';
import "@/assets/setEvent.ts"
import "@/assets/resetStyle.scss"
import '@/styles/index.scss'
import '@nutui/nutui/dist/packages/notify/style/css';
import '@nutui/nutui/dist/packages/toast/style/css';
import '@nutui/nutui/dist/packages/dialog/style/css';
import shrinkOnClick from '@/directives/shrinkOnClick';
import longPress from '@/directives/longPress';
import { Local } from './utils/storage';
// import Vconsole from "vconsole";
import imgT from '@/components/Imgt.vue'
import H5Websocket from "@/net/Net1";
import { MessageMapRegister } from "@/netBase/MessageMapRegister";
import { getFastestUrl } from './utils/publicMethods';
// import Vconsole from "vconsole";
MessageMapRegister.register();
export const getLocale = async (settings: any) => { // Page(pinia).$state.settings

    const adminI18nRes = await fetch(settings.admin_i18n_add_url + '?' + new Date().getTime())
    const adminI18n = await adminI18nRes.json()
    i18n.global.mergeLocaleMessage('zh-CN', adminI18n.zh)
    i18n.global.mergeLocaleMessage('vi-VN', adminI18n.vn)
    i18n.global.mergeLocaleMessage('en-US', adminI18n.en)
    await Page(pinia).setAdminI18n(adminI18n)
}

export const getSetting = async () => {
    // 53测试环境
    // const settingsRes2 = await fetch('https://yuenan-1317897257.cos.ap-singapore.myqcloud.com/yuenan/config_pk_test.json?' + new Date().getTime());

    const settingsRes1 = await fetch('http://18.167.187.79/config_h5_develop.json?' + new Date().getTime())
    const settings = await settingsRes1.json() // todo试图不要本地settings，项目会报错
    //  使用新封装的ws重连 简化逻辑操作



    const homeGameDataRes = await fetch(settings.third_game_manage + '?' + new Date().getTime())
    const homeGameData = await homeGameDataRes.json()
    await Page(pinia).setHomePageGameData(homeGameData)

    localStorage.setItem('chat_url', settings.chat_url)
    const chatSettingsRes = await fetch(settings.chat_setting + '?' + new Date().getTime())
    const chatSettings = await chatSettingsRes.json()

    settings.customer_server = chatSettings.data.info_url; //登录聊天室前获取角色地址
    settings.upload_url = chatSettings.data.upload_url; //图片上传、回显地址

    await Page(pinia).setSettings(settings)
    const ipPort = await getFastestUrl()
    await H5Websocket.conectWebsocket(ipPort)
    // console.log(H5Websocket.instance);

    await getLocale(settings)
    // debugger
    if (Local.get('lang')) {
        i18n.global.locale.value = Local.get('lang')
        await Page(pinia).setLang(Local.get('lang'))
    } else {

        let langObj: any = {
            'en-US': 'en-US',
            en: 'en-US',
            'en-AU': 'en-US',
            zh: 'zh-CN',
            'zh-CN': 'zh-CN',
            vi: 'vi-VN',
            'vi-VN': 'vi-VN'
        }

        i18n.global.locale.value = langObj[navigator.language] || 'vi-VN'
        await Page(pinia).setLang(langObj[navigator.language])
    }
}
await getSetting()


// if (import.meta.env.VITE_NODE_ENV !== "production") {
//     // 测试和开发打开，生产不能打开
//     new Vconsole();
// }
const app = createApp(App);
app.component('ImgT', imgT)
app.directive('shrink-on-click', shrinkOnClick);
app.directive('longpress', longPress);
// app.use(vConsole)
app.use(pinia).use(i18n).use(router).mount('#app');
